package com.foreknow.controller;

import com.foreknow.bean.Admin;
import com.foreknow.service.AdminService;
import com.foreknow.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //在doPost中处理表单数据
        //设置服务器端向客户端响应的内容的类型(MIME)
        resp.setContentType("text/html;charset=utf-8");
        //要使用这个对象PrintWriter中的方法在页面中输出内容
        PrintWriter out = resp.getWriter();

        //如何才能获取到请求的参数
        String username = req.getParameter("email");
        String password = req.getParameter("password");
        //需要调用Model(Service和Dao)
        AdminService service = new AdminServiceImpl();
        Admin admin = service.isValidate(username, password);
        if (admin != null) {
            //out.println("登录成功......");
            //跳转资源(转发)
            req.getRequestDispatcher("/user/manager?q=list").forward(req, resp);
            //重定向
            //        resp.sendRedirect("url");

        } else {
            out.println("登录失败......");
        }
    }
}

